package com.example.lottery.common.interceptor;

import com.example.lottery.common.utils.JWTUtil;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
//添加拦截器
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("user_token");
        log.info("获取token:{}",token);
        String url=request.getRequestURI();
        log.info("获取路径:{}",url);
        //解析令牌
        Claims claims= JWTUtil.parseJWT(token);
        if(claims==null){
            log.error("解析JWT令牌失败!");
            response.setStatus(401);
            return false;
        }
        log.info("解析令牌成功,放行");
        return true;
    }
}
